﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;

namespace Server
{
    public class ChatClientHandler
    {
        private Dictionary<string, string> options;
        //private Socket clientSocket;
        private Message protocolMessage;
        private bool running;
        //private Dictionary<Socket, string> clients;
        public ChatClientHandler(Dictionary<string, string> Options)
        {
            protocolMessage = new Message();
            options = Options;
            running = true;
        }

        public void handleProtocol(object client)
        {
            Socket clientSocket = (Socket)client;
            ChatHandler chatthread = new ChatHandler(clientSocket);

            protocolMessage.sendMessage(clientSocket, "$FRANK,CHAT,CONNECT,OK,");
            int count = 0;
            while (running == true)
            {
                string mess = protocolMessage.recMess(clientSocket);
                if (mess != null)
                {
                    string[] message = mess.Split(',');
                    chatthread.handleChat(message, Connection.Clients);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("MESSAGE ERROR");
                    count++;
                }
                
                if (count == 5)
                {
                    running = false;
                }

            }
        }
    }
}
